package com.xt.base.dao.hibernate.userType;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;


public class StringList extends EmptyUserType implements Serializable{

	private static final long serialVersionUID = -347940912318707611L;
	
	private static final String SPLITTER = "\u000f";   
	
	@SuppressWarnings("unchecked")
	public Object deepCopy(Object value) throws HibernateException {
		if(value == null)return null;
        List<String> targetlist = new ArrayList<String>(); 
        List<String> sourcelist = (List) value;   
        targetlist.addAll(sourcelist);
        return targetlist;   
	}

	public boolean equals(Object x, Object y) throws HibernateException {
        if (x == y)   
            return true;   
        if (x != null && y != null) {   
            List<?> xList = (List<?>) x;   
            List<?> yList = (List<?>) y;   
            if (xList.size() != yList.size())   
                return false;   
            for (int i = 0; i < xList.size(); i++) {   
                String str1 = (String) xList.get(i);   
                String str2 = (String) yList.get(i);   
                if (!str1.equals(str2))   
                    return false;   
            }   
            return true;   
        }   
        return false;   
	}

	public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
        String value = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);   
        if (value != null) {   
            return parse(value);   
        } else {   
            return null;   
        }   
	}

	public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        if (value != null) {   
            String str = assemble((List<?>) value);   
            Hibernate.STRING.nullSafeSet(st, str, index);   
        } else {   
            Hibernate.STRING.nullSafeSet(st, value, index);   
        }   
	}

	public Class<?> returnedClass() {
		return List.class;
	}

	private String assemble(List<?> aliaList){
		if(aliaList == null || aliaList.size() == 0 )return null;
        StringBuffer strBuf = new StringBuffer();   
        for (int i = 0; i < aliaList.size() - 1; i++) {   
            strBuf.append(aliaList.get(i)).append(SPLITTER);   
        }   
        strBuf.append(aliaList.get(aliaList.size() - 1));   
        return strBuf.toString();   
	}

    private List<?> parse(String value) {
    	if(StringUtils.isEmpty(value))return null;
    	String nativestr = "_temp_by_native_code";
       	if(value.startsWith(SPLITTER))value = nativestr + value;
       	if(value.endsWith(SPLITTER))value += nativestr;
        String[] strs = StringUtils.split(value, SPLITTER);
        List<String> list = new ArrayList<String>();   
        for (int i = 0; i < strs.length; i++){
        	String addstr = strs[i];
        	addstr = nativestr.equals(addstr)?"":addstr;
        	list.add(addstr);   
        }
        return list;   
    }   
}
